﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace LdCms.Web.Controllers.MVC.Community
{
    using LdCms.EF.DbModels;
    using LdCms.IBLL.Community;
    using LdCms.Common.Extension;
    using LdCms.Common.Utility;
    using LdCms.Common.Web;
    using LdCms.Web.Models;
    using LdCms.Web.Services;
    using LdCms.Web.Authorizes;
    /// <summary>
    /// 
    /// </summary>
    [AdminAuthorizeAttribute(Roles = "Admins")]
    public class CommunityConfigController : BaseController
    {
        private readonly IBaseManager BaseManager;
        private readonly IConfigService ConfigService;
        public CommunityConfigController(IBaseManager BaseManager, IConfigService ConfigService) : base(BaseManager)
        {
            this.BaseManager = BaseManager;
            this.ConfigService = ConfigService;
        }
        public override IActionResult Index()
        {
            return View();
        }
        public IActionResult Settings()
        {
            try
            {
                var entity = ConfigService.GetConfig(SystemID, CompanyID);
                if (entity == null)
                    return View(new Ld_Community_Config());
                return View(entity);
            }
            catch (Exception ex)
            {
                return ToError(ex.Message);
            }
        }

        [HttpPost]
        [ActionName("Save")]
        public JsonResult SaveConfig()
        {
            try
            {
                string fName = GetFormValue("fName");
                string fTitle = GetFormValue("fTitle");
                string fKeyword = GetFormValue("fKeyword");
                string fDescription = GetFormValue("fDescription");
                string fSendIntervalTime = GetFormValue("fSendIntervalTime");
                string fReplyIntervalTime = GetFormValue("fReplyIntervalTime");
                string fRemark = GetFormValue("fRemark");
                string fIsSend = GetFormValue("fIsSend");
                string fState = GetFormValue("fState");
                var result = ConfigService.UpdateConfig(new Ld_Community_Config()
                {
                    SystemID = SystemID,
                    CompanyID = CompanyID,
                    Name = fName,
                    Title = fTitle,
                    Keyword = fKeyword,
                    Description = fDescription,
                    SendIntervalTime = Utility.IsNum(fSendIntervalTime, 5),
                    ReplyIntervalTime = Utility.IsNum(fReplyIntervalTime, 1),
                    Remark = fRemark,
                    IsSend = fIsSend.ToBool(),
                    State = fState.ToBool(),
                    CreateDate = DateTime.Now
                });
                if (result)
                    return Success("ok");
                else
                    return Error("fail");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }






    }
}